import { defineStore } from 'pinia'
import { ref } from 'vue'
import { store } from '.'

const initUserInfo =  {
  id: '',
  username: '',
  tel: '',
  nickname: '',
  avatar: '',
  age: '',
  sex: ''
}

// 组合api   setup
export const useUserHooks = defineStore('user', () => {
  const token = ref(localStorage.getItem('token') || '')
  const userInfo = ref({ ...initUserInfo })
  const LoginSuccess = (data) => {
    token.value = data.token
    userInfo.value = data.userInfo
  }

  const setUserInfo = (value) => {
    userInfo.value = value
  }

  const QuitLogin = () => {
    token.value = ''
    userInfo.value = initUserInfo
    localStorage.removeItem('token')
    localStorage.removeItem('refreshToken')
  }

  return {
    token,
    userInfo,
    LoginSuccess,
    QuitLogin,
    setUserInfo
  }
})

// 出了 setup 之外的都可以使用
export const useWidthUserHooks = () => {
  return useUserHooks(store)
}
